﻿using Newtonsoft.Json;
using System;

namespace Bilibili.Model
{
    static class Extension
    {
        public static string ToTenThousand(this object par)
        {
            try
            {
                var num = Convert.ToDouble(par);
                if (num >= 10000)
                    return (num / 10000).ToString("0.00") + "万";
                else
                    return num.ToString("0");
            }
            catch
            {
                return "0";
            }
        }
        public static int ToInt32(this object token)
        {
            if (token == null)
                return -1;
            var str = token.ToString();
            try
            {
                if (int.TryParse(str, out int result))
                    return result;
                else
                    return Convert.ToInt32(str);
            }
            catch
            {
                try
                {
                    return Convert.ToInt32(str.Split('.')[0]);
                }
                catch
                {
                    return -1;
                }
            }
        }
        public static T ToObject<T>(this string json)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(json);
            }
            catch { }
            return default;
        }
    }
}